package com.goparry.taskman.inbox;

import java.util.List;

import com.goparry.taskman.R;
import com.goparry.taskman.repository.Task;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView;

// This ArrayAdapter is responsible for displaying each task in a ListView

public class TaskAdapter extends ArrayAdapter<Task> {
	
	int resource;

	public TaskAdapter(Context context, int _resource, int textViewResourceId,
			List<Task> objects) {
		super(context, _resource, textViewResourceId, objects);
		
		//This is a pointer to the layout that displays a task
		resource = _resource; 
	}

	//@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// Get the specific task
		Task task = (Task)this.getItem(position);
		
		// Create a new LinearLayout for this context
		LinearLayout newView = new LinearLayout(getContext());

		// Inflate a new view if this is not an update.
		if (convertView == null) {
			newView = new LinearLayout(getContext());
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
			vi.inflate(resource, newView, true);
		} else {
			newView = (LinearLayout)convertView;
		}
		
		// Populate the drawing resources
		ImageView type = (ImageView)newView.findViewById(R.id.icon);
		if ("work".equals(task.getType())) {
			type.setImageResource(R.drawable.ic_task_work);
		} else if ("review".equals(task.getType())) {
			type.setImageResource(R.drawable.ic_task_review);
		} else if ("approval".equals(task.getType())) {
			type.setImageResource(R.drawable.ic_task_approval);
		} else {
			type.setImageResource(R.drawable.ic_task_work);
		}
		
		TextView name = (TextView)newView.findViewById(R.id.name);
		name.setText(task.getName());
		
		TextView dueDate = (TextView)newView.findViewById(R.id.dueDate);
		dueDate.setText("Due " + task.getDueDate());
		
		TextView title = (TextView)newView.findViewById(R.id.title);
		title.setText(task.getTitle());
		
		TextView numDocs = (TextView)newView.findViewById(R.id.numDocs);
		numDocs.setText(String.valueOf(task.getNumberOfDocs()));
		
		return newView;
	}
}
